using CatLib.Container;
using Cysharp.Threading.Tasks;
using GameFramework;
using GameFramework.Fsm;
using GameFramework.Procedure.Attribute;
using GameFramework.Procedure.Fsm;
using GameFramework.UI;
using GameFramework.UI.FairyGUI;
using GameLogic.UI.Launch;

namespace GameLogic.Launch
{
    /// <summary>
    /// 启动流程
    /// </summary>
    [Procedure(true)]
    public sealed class LaunchProcedure : AFsmProcedure
    {
        private bool m_FormOpened;

        public override void OnEnter(IFsm fsm)
        {
            base.OnEnter(fsm);

            m_FormOpened = false;
            OpenLaunchForm().Forget();
        }

        private async UniTask OpenLaunchForm()
        {
            var code = await UIService.That.OpenUIForm<LaunchForm>();
            if (code != 0)
            {
                Log.Error($"Open LaunchForm failed: {code}");
                return;
            }

            m_FormOpened = true;
        }

        public override void OnUpdate(IFsm fsm, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);

            if (m_FormOpened)
                Change<CheckVersionProcedure>(fsm);
        }
    }
}